/**
 * 筛选条件通用mixin
 * 切换后调用通用方法：loadData()
 */
export const mixinSearch = {
  data() {
    return {
      loading: false,
      // form参数
      queryParam: {},
      // 页码
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
        size: 'small',
        showSizeChanger: true,
        showQuickJumper: true,
        showTotal: (total) => `共 ${total} 条`
      },
      // 排序
      sorter: {}
    }
  },
  methods: {
    /**
     * 动态获取生产tablekey
     * 当前时间戳拼接index序号
     */
    bindRowKey(rec, index) {
      const dateTime = new Date().getTime()
      return `${dateTime}-${index}`
    },

    /**
     * 查询条件
     */
    bindSearch() {
      this.pagination.current = 1
      this.loadData()
    },

    /**
     * 重置搜索
     */
    resetSearch() {
      this.queryParam = {}
      this.pagination.current = 1
      this.loadData()
    },

    /**
     * 切换table页码
     * @param {Object} pagination
     * @param {Object} filters
     * @param {Object} sorter
     */
    handleTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination }

      pager.current = pagination.current
      pager.pageSize = pagination.pageSize
      this.pagination = pager
      this.sorter = sorter
      this.loadData()
    },

    /**
     * 设置默认load事件，页面可覆盖mixins部分
     */
    loadData() {}
  }
}

/**
 * 新增、编辑弹窗mixin
 * @mixin  labelCol         form表单label设置
 * @mixin  wrapperCol       form表单wrapper设置
 * @mixin  visible          控制modal显示
 * @mixin  confirmLoading   提交按钮load状态
 * @mixin  modalType        modal类型，add: 添加; edit: 编辑
 */
export const mixinModal = {
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 }
      },
      layout: 'vertical',
      visible: false,
      confirmLoading: false,
      modalType: 'add'
    }
  },
  methods: {
    /**
     * 点击Modal取消按钮
     * 取消按钮load状态
     * 初始化价格输入框
     */
    handleCancel() {
      this.visible = false
      this.confirmLoading = false
      setTimeout(() => {
        this.$refs['ruleForm'].resetFields()
      }, 400)
    }
  }
}
